<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input
            v-model="query.blurry"
            clearable
            size="small"
            placeholder="模糊搜索"
            style="width: 200px;"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <date-range-picker v-model="query.createTime" class="date-item" />
          <rrOperation />
        </div>
        <crudOperation :permission="permission" />
      </div>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="columnName" label="名称" />
        <el-table-column :show-overflow-tooltip="true" prop="sfqy" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.sfqy"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.sfqy)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <!--   编辑与删除   -->
        <el-table-column
          v-permission="['admin','column:edit','column:del']"
          label="操作"
          width="130px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <column-form />
    </el-card>
  </div>
</template>

<script>
import componentJob from '@/api/system/component';
import CRUD, { presenter, header, crud } from '@crud/crud';
import crudOperation from '@crud/CRUD.operation';
import pagination from '@crud/Pagination';
import udOperation from '@crud/UD.operation';
import rrOperation from '@crud/RR.operation';
import DateRangePicker from '@/components/DateRangePicker/index';
import columnForm from '@/views/system/column/form';

export default {
  name: 'ComponentColumn',
  components: { crudOperation, pagination, udOperation, rrOperation, DateRangePicker, columnForm },
  cruds() {
    return CRUD({
      title: '栏目',
      url: 'rest/api/component',
      crudMethod: { ...componentJob }
    });
  },
  data() {
    return {
      permission: {
        add: ['admin', 'column:add'],
        edit: ['admin', 'column:edit'],
        del: ['admin', 'column:del']
      }
    };
  },
  mixins: [presenter(), header(), crud()],
  methods: {
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.state_type[val] + '" ' + data.columnName + '栏目, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // eslint-disable-next-line no-undef
        componentJob.edit(data).then(() => {
          // eslint-disable-next-line no-undef
          this.crud.notify(this.dict.label.state_type[val] + '成功', 'success');
        }).catch(err => {
          data.enabled = !data.enabled;
          console.log(err.data.message);
        });
      }).catch(() => {
        data.enabled = !data.enabled;
      });
    }
  }
};
</script>

<style scoped>

</style>
